//
//  FKBusinessCooperationVC.m
//  WanTao
//
//  Created by Macbook Pro on 2019/3/22.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKBusinessCooperationVC.h"
#import "FKPlaceholderTextView.h"
#import "FKCityPicker.h"

@interface FKBusinessCooperationVC ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *merchantNameField;
@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UITextField *mobileField;
@property (weak, nonatomic) IBOutlet UITextField *addressField;
@property (weak, nonatomic) IBOutlet FKPlaceholderTextView *detailAddressView;
@property (weak, nonatomic) IBOutlet UITextField *remarkField;
@property (weak, nonatomic) IBOutlet UIButton *saveBtn;

@property (nonatomic, strong) NSMutableDictionary *param;

@end

@implementation FKBusinessCooperationVC

- (instancetype)init
{
    return [UIStoryboard storyboardWithName:@"FKBusinessCooperationVC" bundle:nil].instantiateInitialViewController;
}

- (NSMutableDictionary *)param
{
    if (!_param) {
        _param = [NSMutableDictionary dictionary];
    }
    return _param;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(detailAddressDidChanged:) name:UITextViewTextDidChangeNotification object:nil];
    [self.saveBtn setBackgroundImage:[UIImage imageNamed:@"bg_main"] forState:UIControlStateNormal];
    self.saveBtn.enabled = false;
    self.detailAddressView.placeholder = @"详细地址";
    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag | UIScrollViewKeyboardDismissModeInteractive;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (IBAction)saveBtnClicked:(id)sender
{
    [self.view endEditing:true];
//    if (self.merchantNameField.text.length == 0) {
//        [SVProgressHUD showInfoWithStatus:self.merchantNameField.placeholder];
//        return;
//    }
//    if (self.nameField.text.length == 0) {
//        [SVProgressHUD showInfoWithStatus:self.nameField.placeholder];
//        return;
//    }
//    if (self.mobileField.text.length == 0) {
//        [SVProgressHUD showInfoWithStatus:self.mobileField.placeholder];
//        return;
//    }
    
    self.param[@"seller_name"] = self.merchantNameField.text;
    self.param[@"person"] = self.nameField.text;
    self.param[@"mobile"] = self.mobileField.text;
    self.param[@"det_address"] = self.detailAddressView.text;
    self.param[@"content"] = self.remarkField.text;
    [SVProgressHUD show];
    [FKHttpTool postWithSuffix:@"m=Home&c=Mine&a=cooperation" param:self.param success:^(FKBaseResult *result) {
        if (result.isSuccess) {
            [SVProgressHUD showSuccessWithStatus:@"申请成功，请耐心等待审核"];
            fkPopVC;
        }
    } failure:nil];
}

- (void)detailAddressDidChanged:(NSNotification *)noti
{
    [self check];
}

- (void)check
{
    self.saveBtn.enabled = (self.merchantNameField.text.length > 0 &&
                            self.nameField.text.length > 0 &&
                            self.mobileField.text.length == 11 &&
                            self.addressField.text.length > 0 &&
                            self.detailAddressView.text.length > 0);
}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    [self check];
    NSString *finalText = [textField.text stringByAppendingString:string];
    if ([textField isEqual:self.merchantNameField]) {
        return finalText.length <= 20;
    } else if ([textField isEqual:self.nameField]) {
        return finalText.length <= 20;
    } else if ([textField isEqual:self.mobileField]) {
        return ([finalText fk_isPureInt] && finalText.length <= 11);
    }
    return true;
}

#pragma mark - Table view data source
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 3) {
        FKCityPicker *picker = [[FKCityPicker alloc] init];
        [picker show];
        fkWeakSelf(self);
        picker.selectedCityBlock = ^(NSDictionary *dict) {
            weakself.param[@"province"] = dict[@"province"];
            weakself.param[@"city"] = dict[@"city"];
            weakself.param[@"country"] = dict[@"area"];
            
            weakself.addressField.text = fkFORMAT(@"%@ %@ %@", dict[@"province"], dict[@"city"], dict[@"area"]);
        };
    }
}


@end
